Shader "Unlit/DT_Football_Field"
{
    Properties
    { 
        _MapSizeX("MapSizeX", float) = 147
        _MapSizeY("MapSizeY", float) = 79

        _Color ("Color", Color) = (1,1,1,1)
        _radius("Radius", float) = 25
        _jinquw("Jinqu W", float) = 22
        _jinquh("Jinqu H", float) = 48
        _qiumen("Qiumen", float) = 9

        [Toggle]_viewGrid("ViewGrid", float) = 1 

        _DTlineWidth("LineWidth", float) = 0.01
    }
    SubShader
    {
        Tags { "RenderType"="Opaque" } 
        Pass
        {
            Tags { "LightMode" = "UniversalForward" }
            CGPROGRAM
            #pragma vertex vert
            #pragma fragment frag 

            #include "UnityCG.cginc"


            struct appdata
            {
                float4 vertex : POSITION;
                float2 uv : TEXCOORD0;
            };

            struct v2f
            {
                float2 uv : TEXCOORD0;  
                float4 vertex : SV_POSITION;
                float4 col : COLOR;

            };

            fixed4 _Color;
            float _radius;
            float _jinquw,_jinquh;
            float _qiumen;
            float _MapSizeX;
            float _MapSizeY;
            float _viewGrid;

            float _DTlineWidth;

            
            //绘制点
            float DrawPoint(float2 uv, float4 mapsize,float far)
            {
                float s = far > 0 ? 0.0 : 1.0;
                float2 center = float2(mapsize.x * (abs(far) - 0.5) , 0.5);

                float2 uvToCenter = float2(
                    (abs(s - uv.x) - center.x ) * mapsize.z,
                    (uv.y - center.y   ) * mapsize.w
                );
                float distance = length(uvToCenter);
                float p = 1.0 - step( _DTlineWidth * 0.5,distance);
                return p;
            }


            // 绘制中心圆
            float4 DrawCenterCircle(float2 uv, float4 mapsize)
            {  
                float4 col = float4(0,0,0,1);

                float linewidth = 0.4;
                float bili = mapsize.z / mapsize.w;
                float2 circleCenter = float2(0.5, 0.5);
                float2 uvToCenter = float2(
                    (uv.x - circleCenter.x ) * bili  ,
                    (uv.y - circleCenter.y) 
                );
                float distance = length(uvToCenter);
                float centerpoint = step(  distance,mapsize.y * _DTlineWidth);
                float circle =  step(distance, mapsize.y * 0.5 * _radius)  * step( mapsize.y * 0.5 * (_radius - _DTlineWidth * 2),distance);                
                
                
                
                float waikuang = step(uv.x,mapsize.x);//左
                waikuang += step(1.0 - uv.x ,mapsize.x);//右
                waikuang += step(uv.y,mapsize.y);//下
                waikuang += step(1.0 - uv.y,mapsize.y);//上

                

                // waikuang *= 1.0 - waikuangblack;

                waikuang += step((uv.x - 0.5 + mapsize.x *  ( 0.5 - _DTlineWidth * 0.5 )) , mapsize.x * 0.5 ) * step(((1.0 - uv.x) - 0.5 + mapsize.x * ( 0.5 - _DTlineWidth * 0.5))  , mapsize.x * 0.5 );//中
                waikuang = saturate(waikuang) ;
                col.rgb =  saturate(waikuang * 0.8 + circle + centerpoint);
                return col  ;
            }

            

            float DrawJinqu(float2 uv, float4 mapsize)
            {
                float linewidth =  mapsize.x * _DTlineWidth;
                float linewidthh =  mapsize.y *  _DTlineWidth;

                float c = step(uv.x,mapsize.x * (_jinquw + 1) );//左
                c += step(1.0 - uv.x,mapsize.x * (_jinquw + 1));//右
                c *= step( mapsize.y * (mapsize.w - _jinquh) * 0.5,uv.y) ; 
                c *= step( mapsize.y * (mapsize.w - _jinquh) * 0.5,1.0 - uv.y);


                float c0 = step( mapsize.x * (_jinquw + 1) - linewidth,uv.x);//上
                c0 *= step( mapsize.x * (_jinquw + 1) - linewidth,1.0 - uv.x);//下
                c0 += step(uv.y, mapsize.y * (mapsize.w - _jinquh) * 0.5 + linewidthh) ;                 
                c0 += step(1.0 - uv.y, mapsize.y * (mapsize.w - _jinquh) * 0.5  + linewidthh);
                return saturate(c * c0 ) * 0.8;

            }

            float Drawqiumen(float2 uv, float4 mapsize)
            { 
                float c = step(uv.x,mapsize.x  );//左
                c += step(1.0 - uv.x,mapsize.x  );//右 
                c *= step( mapsize.y * (mapsize.w - _qiumen) * 0.5,uv.y) ; 
                c *= step( mapsize.y * (mapsize.w - _qiumen) * 0.5,1.0 - uv.y);
                return saturate(c);
            }

            float DrawFZ(float2 _uv, float4 mapsize)
            {
                // return 1; 
                float2 oov = frac(mapsize.zw * 0.1) * 10.0  ;
                oov.x = -floor(oov.x * 0.5);
                oov.y = floor((10 - oov.y) * 0.5) ; 
                float2 uv = (_uv + mapsize.xy * oov ) * (mapsize.zw * 0.1) ;
                
                float grid = 0.0;

                float vx = abs(floor(uv.x ) % 2.0);
                float vy = abs(floor(uv.y ) % 2.0); 

                grid += saturate(vx + 0.5 ) * saturate(vy * 0.5 + 0.4);
                return grid * 0.8 + 0.2;
            }
             
            float DrawLineGrid(float2 _uv, float4 mapsize)
            {                 
                float2 uv = frac(_uv * mapsize.zw);
                float lineWidth = 0.05;
                float grid = 0.0;
                grid += step(uv.x, lineWidth);
                grid += step(uv.y, lineWidth);
                grid += step(1.0 - uv.x, lineWidth);
                grid += step(1.0 - uv.y, lineWidth);

                uv = _uv * mapsize.zw * 1;
                float grid0 = floor(uv.x) % 2;
                grid0 =abs( (floor(uv.y) % 2) - grid0); 
                return  saturate(grid) * _viewGrid  ;
            }

            v2f vert (appdata v)
            {
                v2f o = (v2f)0;
                o.vertex = UnityObjectToClipPos(v.vertex);
                o.uv.xy = v.uv;
                o.uv.y = 1.0 - o.uv.y;
                return o;
            }
            fixed4 frag (v2f i) : SV_Target
            {                
                float w = _MapSizeX + 2;
                float h = _MapSizeY + 2;
                const float4 mapSize = float4(1.0 / w, 1.0 / h, w, h);  
                float2 uv = i.uv.xy; 
                float4 CenterCircle = DrawCenterCircle(uv.xy, mapSize);
                
                
                float3 qiuchang = float3(0,0,0);
                qiuchang.rgb += CenterCircle;
                qiuchang.rgb += DrawPoint(uv.xy, mapSize,11 + 1);
                qiuchang.rgb += DrawPoint(uv.xy, mapSize,-11 - 1); 
                qiuchang.rgb *= 1.0 -Drawqiumen(uv.xy, mapSize);    
                qiuchang.r += Drawqiumen(uv.xy, mapSize);
                qiuchang.rgb += DrawJinqu(uv.xy, mapSize);

                float linewidth =  0.6;
                float waikuangblack = step(uv.x,mapSize.x * linewidth);//左
                waikuangblack += step(1.0 - uv.x ,mapSize.x * linewidth);//右
                waikuangblack += step(uv.y,mapSize.y * linewidth);//下
                waikuangblack += step(1.0 - uv.y, mapSize.y * linewidth);//上


                qiuchang = saturate(qiuchang );


                float3 qiuchangColor = _Color.rgb;
                qiuchangColor *= DrawFZ(uv.xy, mapSize) * (1.0 - qiuchang.r);


                float grid = DrawLineGrid(uv.xy, mapSize)  ;
                
                fixed4 col = 1.0;
                col.rgb = qiuchang + qiuchangColor;
                col.rgb *= (1.5 - (grid * (1.0 - col.r)));
                col.rgb *= (1.0 - waikuangblack);
                clip((1.0 - waikuangblack) - 0.5);
                return saturate(col);
            }
            ENDCG
        }
        
    }
}
